/**
 * Copyright HCP Technology 
 * www.hcp.vn
 */
package models;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.Blob;

import play.data.binding.As;

import siena.Column;
import siena.Filter;
import siena.Id;
import siena.Index;
import siena.Model;
import siena.Query;
import util.Util;

public class ContentModel extends Model implements Serializable{
	
	@Id
	public Long id;
	@Index(value = "user_index")
	public String user;
	public String title;
	public String alias;
	public String description;
	public Date date;
	public boolean active;
	public boolean invisible;
	public String ip;
    public String path;
	
	@Index(value = "category_index")
	public String category;
	
	@Filter("content")
    public static Query<CommentModel> comments;
	
	public ContentModel(String picture, String category, String user, String title, String alias, String description, boolean active, boolean invisible, String ip){
		this.user = user;
		this.title = title;
		this.alias = alias;
		this.description = description;
		this.active = active;
		this.invisible = invisible;
		this.path = picture;
		this.category = category;
		this.date = new Date();
		this.ip = ip;
	}
	
	static Query<ContentModel> all(){
		return Model.all(ContentModel.class);
	}
	
	/**get Content by Id with active/deactive**/
	public static ContentModel findById(Long id, boolean active){
		return all().filter("id", id).filter("active", active).get();
	}
	
	
	/**get Content by Id**/
	public static ContentModel findById(Long id){
		return all().getByKey(id);
	}
	
	public static List<ContentModel> findByLimit(int page, int limit){
		return all().order("-date").fetch(limit, (page - 1) * Util.PAGE_ITEM);
	}
	
	/**get List Content by user with active/deactive**/
	public static List<ContentModel> findByUser(String user, boolean active){
		return all().filter("user", user).filter("active", active).fetch();
	}
	
	/**get List Content by user with active/deactive**/
	public static List<ContentModel> findByUser(String user){
		return all().filter("user", user).order("-date").fetch();
	}
	
	/**get List new Content by limit with active/deactive**/
	public static List<ContentModel> listNewContent(boolean active, int limit){
		if(limit == 0){
			return all().filter("active", active).order("-date").fetch();	
		}else{
			return all().filter("active", active).order("-date").fetch(limit);
		}
	}
	
	/**delete content**/
	public static void deleteContent(Long id){
		try {
			all().getByKey(id).delete();	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*get all content */
	public static List<ContentModel> getAll(){
		return all().order("-date").fetch();
	}
	
	/**get list comments**/
	public static List<CommentModel> listComments(Long id){
		return comments.filter("id", id).order("-date").fetch();
	}
	
	/*get list category*/
	public static List<ContentModel> findByCategory(String category, boolean active){
		return all().filter("category", category).filter("active", active).order("-date").fetch(2);
	}

	/**
	 * @param category2
	 * @return
	 */
	public static int totalCategory(String category) {
		return all().filter("category", category).filter("active", true).order("-date").count();
	}
}
